package com.ht.impl.myactions;

import com.ht.aems.swing.util.MyAction;
import com.ht.impl.ResourceTypeCons;
import com.ht.impl.dialogs.FlashConfigSetter;
import com.mxgraph.model.mxCell;
import com.mxgraph.util.mxUtils;
import com.mxgraph.swing.mxGraphComponent;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Map;

/**
 * @author Wang jiaxiang
 * Date: 2010-7-7
 */
public class SetStyle extends MyAction implements ResourceTypeCons {
    public void actionPerformed(ActionEvent e) {
        mxGraphComponent component = (mxGraphComponent) e.getSource();
        Object[] cells = component.getGraph().getSelectionCells();
        if (cells.length == 0)
            return;
        Map<String, Map<Object, Object>> styles = FlashConfigSetter.showSettingDialog((mxCell) cells[0]);
        if(styles == null )return;
        mxCell cell;
        for (Object o : cells) {
            cell = (mxCell) o;
            Element ele;
            Document doc = mxUtils.createDocument();
            if (!(cell.getValue() instanceof Element) || cell.getValue() == null)
                ele = doc.createElement("Element");
            else
                ele = (Element) cell.getValue();
            String style = "";
            for (String key : styles.keySet()) {
                style += key + MARK_AT;
                Object value;
                int i = 0;
                for (Object obj : styles.get(key).keySet()) {
                    if (i != 0)
                        style += MARK_AND;
                    style += obj.toString() + MARK_MAPPING;
                    value = styles.get(key).get(obj);
                    if (value instanceof Color)
                        style += mxUtils.hexString((Color) value);
                    else
                        style += value.toString();
                    i++;
                }
                style += MARK_SPLIT;
            }
            if(!style.equals(""))
                ele.setAttribute(SPECIAL_DEFINE, style);
            cell.setValue(ele);
        }
    }
}
